package com.ruoyi.engine.controller;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.service.UserRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.engine.domain.BpmProcessDefinition;
import com.ruoyi.engine.service.IBpmProcessDefinitionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模版引擎管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-25
 */
@RestController
@RequestMapping("/engine/definition")
public class BpmProcessDefinitionController extends BaseController
{
    @Autowired
    private IBpmProcessDefinitionService bpmProcessDefinitionService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 查询模版引擎管理列表
     */
//    @PreAuthorize("@ss.hasPermi('engine:definition:list')")
//    @PreAuthorize("@ss.hasRole('a')")
    @GetMapping("/list")
    public TableDataInfo list(BpmProcessDefinition bpmProcessDefinition)
    {
//        SecurityUtils
        List<SysRole> roleList = SecurityUtils.getLoginUser().getUser().getRoles();
        Collection<String> roles = roleList.stream().map(SysRole::getRoleKey).collect(Collectors.toSet());

        System.out.println(roles);

        startPage();
        List<BpmProcessDefinition> list = bpmProcessDefinitionService.selectBpmProcessDefinitionList(bpmProcessDefinition);
        return getDataTable(list);
    }

    /**
     * 导出模版引擎管理列表
     */
    @PreAuthorize("@ss.hasPermi('engine:definition:export')")
    @Log(title = "模版引擎管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BpmProcessDefinition bpmProcessDefinition)
    {
        List<BpmProcessDefinition> list = bpmProcessDefinitionService.selectBpmProcessDefinitionList(bpmProcessDefinition);
        ExcelUtil<BpmProcessDefinition> util = new ExcelUtil<BpmProcessDefinition>(BpmProcessDefinition.class);
        util.exportExcel(response, list, "模版引擎管理数据");
    }

    /**
     * 获取模版引擎管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('engine:definition:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bpmProcessDefinitionService.selectBpmProcessDefinitionById(id));
    }

    /**
     * 新增模版引擎管理
     */
    @PreAuthorize("@ss.hasPermi('engine:definition:add')")
    @Log(title = "模版引擎管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BpmProcessDefinition bpmProcessDefinition)
    {
        //根据key查询最大版本
        Integer maxVersion = bpmProcessDefinitionService.selectMaxVersion();
        if(maxVersion==null){
            maxVersion=1;
        }
        bpmProcessDefinition.setVersion(Long.valueOf(maxVersion));
        return toAjax(bpmProcessDefinitionService.insertBpmProcessDefinition(bpmProcessDefinition));
    }

    /**
     * 修改模版引擎管理
     */
    @PreAuthorize("@ss.hasPermi('engine:definition:edit')")
    @Log(title = "模版引擎管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BpmProcessDefinition bpmProcessDefinition)
    {
        return toAjax(bpmProcessDefinitionService.updateBpmProcessDefinition(bpmProcessDefinition));
    }

    /**
     * 删除模版引擎管理
     */
    @PreAuthorize("@ss.hasPermi('engine:definition:remove')")
    @Log(title = "模版引擎管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bpmProcessDefinitionService.deleteBpmProcessDefinitionByIds(ids));
    }
}
